package com.example.bnuzzu.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bnuzzu.common.dto.HouseListDto;
import com.example.bnuzzu.common.dto.OrderListDto;
import com.example.bnuzzu.entity.Orderinfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author BNUZZU
 * @since 2021-04-13
 */
@Repository
public interface OrderinfoMapper extends BaseMapper<Orderinfo> {

    @Select("SELECT o.OrderID,o.OwnerID,o.TenantID,o.HouseID,o.OrderStatus,o.Version " +
            "FROM orderinfo o ")
    IPage<OrderListDto> selectPageVo(Page<?> page, Integer state);

    @Select("SELECT t1.*,t2.UserName as ownername,t3.UserName as tenantname " +
            "FROM (SELECT * FROM orderinfo) t1 " +
            "LEFT JOIN userinfo t2 on t1.OwnerID = t2.UserID " +
            "LEFT JOIN userinfo t3 on t1.TenantID = t3.UserID " +
            "ORDER BY OrderStatus,DateUpdated DESC")
    List<OrderListDto> getAllOrder();

    /**
     * 自己是房东的订单
     * @param userid
     * @return 结果
     */
    @Select("SELECT orders.*,houses.HouseName,users.UserName as Ownername,(SELECT UserName FROM userinfo WHERE UserID = orders.TenantID) as Tenantname FROM orderinfo orders,houseinfo houses,userinfo users " +
            "WHERE orders.HouseID = houses.HouseID AND orders.OwnerID = users.UserID " +
            "AND users.UserID = #{userid } ORDER BY orders.dateupdated DESC")
    List<OrderListDto> selectOwnOrderInfos(Integer userid);

    /**
     * 自己是租友的订单
     * @param userid
     * @return 结果
     */
    @Select("SELECT orders.*,houses.HouseName,users.UserName as Tenantname,(SELECT UserName FROM userinfo WHERE UserID = orders.OwnerID) as Ownername FROM orderinfo orders,houseinfo houses,userinfo users " +
            "WHERE orders.HouseID = houses.HouseID AND orders.TenantID = users.UserID " +
            "AND users.UserID = #{userid} ORDER BY orders.dateupdated DESC")
    List<OrderListDto> selectTenantOrderInfos(Integer userid);

    /**
     * 查询
     */
    public Orderinfo selectOrderinfoById(@Param(value="OrderID")Long OrderID);

    /**
     * 管理员查询
     */
    public List<Orderinfo> selectOrderinfoList(@Param("Orderinfo") Orderinfo orderinfo);

    /**
     * 用户查询
     */
    public List<Orderinfo> selectOrderinfoByTenantID(@Param(value="TenantID")int TenantID);


    /**
     * 新增
     */
    public int insertOrderinfo(@Param("Orderinfo") Orderinfo orderinfo);

    /**
     * 修改
     */
    public int updateOrderinfo(@Param("Orderinfo") Orderinfo orderinfo);

    /**
     * 删除
     */
    public int deleteOrderinfoById(@Param(value="OrderID")Long OrderID);

}
